package com.yanqu.road.server.cmd.activity.daomu.pvp;

import com.yanqu.road.entity.activity.daomu.config.DaoMuActivityConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.DaoMuPvpProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.daomu.DaoMuActivity;
import com.yanqu.road.server.manager.activity.daomu.DaoMuActivityMgr;
import com.yanqu.road.server.manager.activity.daomu.pb.DaoMuActivityPvpPb;
import com.yanqu.road.server.manager.activity.daomu.pvp.DaoMuActivityPvp;
import com.yanqu.road.entity.activity.daomu.pvp.Coordinate;
import com.yanqu.road.entity.activity.daomu.pvp.DaoMuPvpPosition;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = Protocol.C_CROSS_DAOMU_PVP_OPERATE, desc = "玩家操作")
public class DaoMuPvpOperateCmd extends CrossCommand {

    private int activityId;

    private long playerId;

    private DaoMuPvpPosition position;

    private int type;

    private String param = "";

    private long time;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        DaoMuPvpProto.DaoMuPvpOperateReqMsg reqMsg = DaoMuPvpProto.DaoMuPvpOperateReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        if (reqMsg.hasPosition()) {
            position = DaoMuActivityPvpPb.parseDaoMuActivityPvpPosition(reqMsg.getPosition());
        }
        if (reqMsg.hasType()){
            type = reqMsg.getType();
        }
        if (reqMsg.hasParam()) {
            param = reqMsg.getParam();
        }
        if (reqMsg.hasTime()) {
            time = reqMsg.getTime();
        }
        playerId = packet.getPlayerId();
        return true;
    }


    @Override
    public void execute(YanQuMessage packet) throws Exception {
        DaoMuActivity activity = DaoMuActivityMgr.getActivity(activityId);
        if (activity == null) {
            ret = GameErrorCode.E_DAOMU_ACTIVITY_NOT_EXIST;
            return;
        }
        DaoMuActivityPvp pvp = activity.getDaoMuActivityPvp();
        if (pvp == null) {
            ret = GameErrorCode.E_DAOMU_PVP_NOT_OPEN;
            return;
        }
        DaoMuActivityConfig config = pvp.getConfig();
        if (config == null) {
            ret = GameErrorCode.E_DAOMU_PVP_CONFIG_NOT_FOUND;
            return;
        }
        if (type == 1) {
            // 移动
            ret = pvp.moveByCmd(playerId, position, "1".equals(param));
        } else if (type == 2) {
            // 发子弹 (废弃)
        } else if (type == 3) {
            // 复活
            ret = pvp.reviveByCmd(playerId);
        } else if (type == 4) {
            // 捡物资
            ret = pvp.getRoomGoodsByCmd(playerId, param, time);
        } else if (type == 5) {
            // 开技能
            ret = pvp.openSkillByCmd(playerId);
        } else if (type == 6) {
            // 选技能
            ret = pvp.chooseSkillByCmd(playerId, Integer.parseInt(param));
        } else if (type == 7) {
            // 打怪,  子弹id#怪物id#x#y
            String[] split = param.split("#");
            String bulletId = split[0];
            String monsterId = split[1];
            double monsterXPos = Double.parseDouble(split[2]) / 10000;
            double monsterYPos =  Double.parseDouble(split[3]) / 10000;
            pvp.attackMonsterByCmd(playerId, bulletId, monsterId, new Coordinate(monsterXPos, monsterYPos));
        } else if (type == 8) {
            // 获取当前房间的宝箱
            ret = pvp.getRoomBoxUidByCmd(playerId);
        }

    }

    @Override
    public void sendData() {
        if (ret != 0) {
            DaoMuPvpProto.DaoMuPvpOperateRespMsg.Builder respMsg = DaoMuPvpProto.DaoMuPvpOperateRespMsg.newBuilder();
            respMsg.setRet(ret);
            respMsg.setType(type);
            MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.U_DAOMU_PVP_OPERATE, respMsg));
        }
    }
}
